<?php
defined('IN_IXDCW') or exit('Access Denied');
require DT_CORE.'/'.$module.'/common.inc.php';
$MG['sell_limit'] > -1 or Prompt::alert(lang('message->without_permission_and_upgrade'), 'goback');
$MTYPE = get_type('product-'.$_userid);

require_once DT_INC.'/post.func.php';
include load($module.'.lang');
include load('my.lang');
$do = new SellModule($moduleid);

$sql = $_userid ? "username='$_username'" : "ip='$DT_IP'";
$limit_used = $limit_free = $need_password = $need_captcha = $need_question = $fee_add = 0;
if(in_array($action, array('', 'add'))) {
    $r = $db->getOne("SELECT COUNT(*) AS num FROM {$table} WHERE $sql AND status>1");
    $limit_used = $r['num'];
    $limit_free = $MG['sell_limit'] > $limit_used ? $MG['sell_limit'] - $limit_used : 0;
}

if(check_group($_groupid, $MOD['group_refresh'])) $MOD['credit_refresh'] = 0;
$suffix = '';
if($catid)$suffix .='&catid='.$catid;
if($com)$suffix .='&com='.$com;
switch($action) {
    case 'add':
	    $userinfo = userinfo($_username);
	    if($MG['sell_limit'] && $limit_used >= $MG['sell_limit']) Prompt::alert(lang($L['info_limit'], array($MG[$MOD['module'].'_limit'], $limit_used)), $_userid ? url('member/my').'?mid='.$mid : url('member/my'));
        if($MG['day_limit']) {
            $today = strtotime(timetodate($DT_TIME, 3).' 00:00:00');
            $r = $db->getOne("SELECT COUNT(*) AS num FROM {$table} WHERE $sql AND addtime>$today");
            if($r && $r['num'] >= $MG['day_limit']) Prompt::alert(lang($L['day_limit'], array($MG['day_limit'])), $_userid ? url('member/my').'?mid='.$mid : url('member/my'));
        }

        $fee_currency = $MOD['fee_currency'];
        $fee_unit = $fee_currency == 'money' ? $DT['money_unit'] : $DT['credit_unit'];
        $need_password = $fee_add && $fee_currency == 'money';
        $need_captcha = $MOD['captcha_add'] == 2 ? $MG['captcha'] : $MOD['captcha_add'];
        $need_question = $MOD['question_add'] == 2 ? $MG['question'] : $MOD['question_add'];
        $could_elite = check_group($_groupid, $MOD['group_elite']) && $MOD['credit_elite'] && $_userid;
        $could_color = check_group($_groupid, $MOD['group_color']) && $MOD['credit_color'] && $_userid;

        if($post) {
           // if($fee_add && $fee_add > ($fee_currency == 'money' ? $_money : $_credit)) Prompt::alert($L['balance_lack']);
           // if($need_password && !is_payword($_username, $password)) Prompt::alert($L['error_payword']);

            if(!$_userid) {
                if(strlen($post['company']) < 10) Prompt::alert($L['type_company']);
                if($AREA) {
                    if(!isset($AREA[$post['areaid']])) Prompt::alert($L['type_area']);
                } else {
                    if(!$post['areaid']) Prompt::alert($L['type_area']);
                }
                if(strlen($post['truename']) < 4) Prompt::alert($L['type_truename']);
                if(strlen($post['mobile']) < 7) Prompt::alert($L['type_mobile']);
            }

            if($MG['add_limit']) {
                $last = $db->getOne("SELECT addtime FROM {$table} WHERE $sql ORDER BY itemid DESC");
                if($last && $DT_TIME - $last['addtime'] < $MG['add_limit']) Prompt::alert(lang($L['add_limit'], array($MG['add_limit'])));
            }
            $msg = captcha($captcha, $need_captcha, true);
            if($msg) Prompt::alert($msg);
            $msg = question($answer, $need_question, true);
            if($msg) Prompt::alert($msg);

            if($do->pass($post)) {
	            $post['city'] = explode(',',$post['city']);
	            $return = push_charge($_groupid,$userinfo,$post['city'],$moduleid);
	            if(!$return['success'] && $return['total'] > 0){
		            $ajaxReturn =array('success'=>$return['success'],'total'=>$return['total']);
		            exit(json_encode($ajaxReturn));
	            };
	            $post['total'] = $return['total'];
                $CAT = get_cat($post['catid']);
                if(!$CAT || !check_group($_groupid, $CAT['group_add'])) Prompt::alert(lang($L['group_add'], array($CAT['catname'])));
                if($MOD['upload_thumb'] && $_userid && strlen($post['thumb']) < 5) Prompt::alert($L['sell_upload_image']);
                $post['addtime'] = $post['level'] = $post['fee'] = 0;
                $post['style'] = $post['template'] = $post['note'] = $post['filepath'] = '';
                if(!$IMVIP && $MG['uploadpt']) $post['thumb1'] = $post['thumb2'] = '';
                $need_check =  $MOD['check_add'] == 2 ? $MG['check'] : $MOD['check_add'];
                $post['status'] = get_status(3, $need_check);
                $post['hits'] = 0;
                $post['username'] = $_username;

                $do->add($post);

                $msg = $post['status'] == 2 ? $L['success_check'] : $L['success_add'];
                if($_userid) {
                    Cookie::set('dmsg', $msg);
                    $forward = url('member/my').'?mid='.$mid.'&status='.$post['status'].$suffix;
                    Prompt::alert('信息发布成功', '', 'parent.window.location="'.$forward.'";');
                } else {
                    Prompt::alert($msg, '', 'parent.window.location=parent.window.location;');
                }
            } else {
                Prompt::alert($do->getErrmsg(), '', ($need_captcha ? reload_captcha() : '').($need_question ? reload_question() : ''));
            }
        } else {
            if($itemid) {
                $MG['copy'] && $_userid or Prompt::alert(lang('message->without_permission_and_upgrade'), 'goback');
                $do->setItemid($itemid);
                $r = $do->getOne();
                if(!$r || $r['username'] != $_username) Prompt::message();
                extract($r);
                $thumb = $thumb1 = $thumb2 = '';
                $totime = $totime > $DT_TIME ? timetodate($totime, 3) : '';
            } else {
                foreach($do->getFields() as $v) {
                    $$v = '';
                }
                $content = '';
                $catid = 0;
                $days = 3;
                $totime = '';
                $typeid = 0;
            }
            $item = array();
            $mycatid_select = type_select('product-'.$_userid, 0, 'post[mycatid]', $L['type_default']);
        }
        break;
    case 'edit':
        $itemid or Prompt::message();
        $do->setItemid($itemid);
        $item = $do->getOne();
        if(!$item || $item['username'] != $_username) Prompt::message();

        if($MG['edit_limit'] < 0) Prompt::message($L['edit_refuse']);
        if($MG['edit_limit'] && $DT_TIME - $item['addtime'] > $MG['edit_limit']*86400) Prompt::message(lang($L['edit_limit'], array($MG['edit_limit'])));

        if($post) {
            if($do->pass($post)) {
                $CAT = get_cat($post['catid']);
                if(!$CAT || !check_group($_groupid, $CAT['group_add'])) Prompt::alert(lang($L['group_add'], array($CAT['catname'])));
                $post['addtime'] = timetodate($item['addtime']);
                $post['level'] = $item['level'];
                $post['fee'] = $item['fee'];
                $post['style'] = $item['style'];
                $post['template'] = $item['template'];
                $post['filepath'] = $item['filepath'];
                $post['note'] = $item['note'];
                if(!$IMVIP && $MG['uploadpt']) {
                    $post['thumb1'] = $item['thumb1'];
                    $post['thumb2'] = $item['thumb2'];
                }
                $need_check =  $MOD['check_add'] == 2 ? $MG['check'] : $MOD['check_add'];
                $post['status'] = get_status($item['status'], $need_check);
                $post['hits'] = $item['hits'];
                $post['username'] = $_username;
                $do->edit($post);

                Cookie::set('dmsg', $L['success_edit']);
                Prompt::alert('', '', 'parent.window.location="'.$forward.'"');
            } else {
                Prompt::alert($do->getErrmsg());
            }
        } else {
            extract($item);
	        $city = trim($city,'|');
	        $city = explode('|',$city);
            $totime = $totime ? timetodate($totime, 3) : '';
            $mycatid_select = type_select('product-'.$_userid, 0, 'post[mycatid]', $L['type_default'], $mycatid);
        }
        break;
    case 'delete':
        $MG['delete'] or Prompt::message();
        $itemid or Prompt::message();
        $itemids = is_array($itemid) ? $itemid : array($itemid);
        foreach($itemids as $itemid) {
            $do->setItemid($itemid);
            $item = $do->getOne();
            if(!$item || $item['username'] != $_username) Prompt::message();
            $do->recycle($itemid);
        }
        dmsg($L['success_delete'], $forward);
        break;
    case 'refresh':
        $MG['refresh_limit'] > -1 or Prompt::alert(lang('message->without_permission_and_upgrade'), 'goback');
        $itemid or Prompt::message($L['select_info']);
        $itemids = $itemid;
        $s = $f = 0;
        foreach($itemids as $itemid) {
            $do->setItemid($itemid);
            $item = $do->getOne();
            $could_refresh = $item && $item['username'] == $_username;
            if($could_refresh && $MG['refresh_limit'] && $DT_TIME - $item['edittime'] < $MG['refresh_limit']) $could_refresh = false;
            if($could_refresh && $MOD['credit_refresh'] && $MOD['credit_refresh'] > $_credit) $could_refresh = false;
            if($could_refresh) {
                $do->refresh($itemid);
                $s++;
                if($MOD['credit_refresh']) $_credit = $_credit - $MOD['credit_refresh'];
            } else {
                $f++;
            }
        }
        if($MOD['credit_refresh'] && $s) {
            $credit = $s*$MOD['credit_refresh'];
            credit_add($_username, -$credit);
            credit_record($_username, -$credit, 'system', lang($L['credit_record_refresh'], array($MOD['name'])), lang($L['refresh_total'], array($s)));
        }
        $msg = lang($L['refresh_success'], array($s));
        if($f) $msg = $msg.' '.lang($L['refresh_fail'], array($f));
        dmsg($msg, $forward);
        break;
    default:
        $status = isset($status) ? intval($status) : 3;
        in_array($status, array(1, 2, 3, 4)) or $status = 3;
        $typeid = isset($typeid) ? ($typeid === '' ? -1 : intval($typeid)) : -1;
        $mycatid = isset($mycatid) ? ($mycatid === '' ? -1 : intval($mycatid)) : -1;
        $mycat_select = type_select('product-'.$_userid, 0, 'mycatid', $L['type_default'], $mycatid, '', $L['type_my']);

        $condition = "username='$_username' AND status=$status";
        if($keyword) $condition .= " AND keyword LIKE '%$keyword%'";
        if($catid) $condition .= $CAT['child'] ? " AND catid IN (".$CAT['arrchildid'].")" : " AND catid=$catid";
        if($typeid >= 0) $condition .= " AND typeid=$typeid";
        if($mycatid >= 0) $condition .= " AND mycatid=$mycatid";

        $timetype = strpos($MOD['order'], 'add') !== false ? 'add' : '';
        $lists = $do->getList($condition, $MOD['order']);
        foreach($lists as $k=>$v) {
            $lists[$k]['mycat'] = $v['mycatid'] && isset($MTYPE[$v['mycatid']]) ? set_style($MTYPE[$v['mycatid']]['typename'], $MTYPE[$v['mycatid']]['style']) : $L['type_default'];
        }
        break;
}
$head_title = lang($L['module_manage'], array($MOD['name']));
if($_userid) {
    $nums = array();
    for($i = 1; $i < 5; $i++) {
        $r = $db->getOne("SELECT COUNT(*) AS num FROM {$table} WHERE username='$_username' AND status=$i");
        $nums[$i] = $r['num'];
    }
    $nums[0] = count($MTYPE);
}
include template('my_'.$module, 'member');
?>